package caiwei.android_study_sample.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import caiwei.android_study_sample.R;

public class Android07UIBaseSampleListview extends ListActivity
{
	private EditText mEditText  ;
	List<String> itemsList ;
	ArrayAdapter<String> adapter ;
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		itemsList.remove(position)  ;
		adapter.notifyDataSetChanged()  ;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listviewsample) ;
		displayList() ;
	}
	

	
	private void displayList()
	{
		itemsList = new ArrayList<String>()  ;
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,itemsList)  ;
		setListAdapter(adapter) ;
		
		mEditText = (EditText)findViewById(R.id.editTextlistviewSample)  ;
		mEditText.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if(keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP)
				{
					itemsList.add(mEditText.getText().toString())  ;
					adapter.notifyDataSetChanged()  ;
					mEditText.setText(null) ;
				}
				return false;
			}
		})  ;
		
	}
}
